﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;

public partial class BasicBase_SoldierArchive_EmployeeCardStat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }

    void init()
    {
        UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
        if (userInfo.DepartmentID.Substring(0, 4) == "1300")
        {
            ddlDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList("(SUBSTRING(DepartmentID, 5, 3) = '000') OR (DepartmentID = '1313100') OR (DepartmentID = '1312100')");
        }
        else
        {
            ddlDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList("(DepartmentID='" + userInfo.DepartmentID.Substring(0, 4) + "000') ");
        }

        ddlDepartment.DataTextField = "FullName";
        ddlDepartment.DataValueField = "DepartmentID";
        ddlDepartment.DataBind();

    }

    void BindGV(int pageIndex)
    {
        string Where = "1<>1";
        if (ddlDepartment.SelectedValue.Length > 0)
        {
            Where = "IsDelete=0 and IsContract=1 and Left(DepartmentID,4)='" + ddlDepartment.SelectedValue.Substring(0, 4) + "'";

            string departmentid = ddlDepartment.SelectedValue;
            Soldier soldier = new Soldier();
            int count = soldier.GetSoldiersCountByWhere(Where);
            ListPageContral1.ItemCount = count;
            List<SoldierArchiveInfo> list = soldier.GetSoldiersByWhere(Where, "DepartmentID,CardID", "Asc", ListPageContral1.PageSize, pageIndex);
            List<SoldierArchiveInfo> list2 = soldier.GetSoldiersByWhere(Where, "DepartmentID", "Asc", count, 0);
            CardCodeInfo card = Yipusoft.SoldierArchives.BLL.CardCode.GetCardCodeInfoByDepartmentID(ddlDepartment.SelectedValue);
            if (card.ContractEstablishment != 0)
            {
                Establishment.Text = Convert.ToString(Convert.ToInt32(card.EmEndNum) - Convert.ToInt32(card.EmStartNum));
                //已使用
                string UseWhere = Where + " and  len(CardID)=8 and left(CardID,4)='" + card.EmFrontCode + "' and convert(int,right(CardID,4),4)>= " + Convert.ToInt32(card.EmStartNum) + " and convert(int,right(CardID,4),4)<=" + Convert.ToInt32(card.EmEndNum);
                int userCount = soldier.GetSoldiersCountByWhere(UseWhere);

                UseCount.Text = userCount.ToString();
                UseCount.NavigateUrl = "SoldierResult.aspx?Where=" + UseWhere;
                //未使用
                NoUserCount.Text = Convert.ToString(Convert.ToInt32(Establishment.Text) - userCount);
                NoUserCount.NavigateUrl = "NoUseCardIds.aspx?where=" + UseWhere + "&Departmentid=" + departmentid + "&IsContract=1";
                //不正确
                string ErrorWhere = " isDelete=0 and iscontract=1 and left(departmentid,4)='" + departmentid.Substring(0, 4) + "' and (len(CardID)<>8  or convert(int,right(CardID,4),4)< " + card.EmStartNum + " or convert(int,right(CardID,4),4)>" + card.EmEndNum + " or left(CardID,4)<>'" + card.EmFrontCode + "' )";

                int errorCount = soldier.GetSoldiersCountByWhere(ErrorWhere);
                ErrorCount.Text = errorCount.ToString();
                ErrorCount.NavigateUrl = "SoldierResult.aspx?Where=" + ErrorWhere;
                gvSoldiers.DataSource = list;
                gvSoldiers.DataBind();

                //重号
                string Repeatwhere = "isDelete=0 and iscontract=1 and left(departmentid,4)='" + departmentid.Substring(0, 4) + "'";
                Repeat.Text = soldier.GetRepeatCardIDList(Repeatwhere).Count.ToString();
                Repeat.NavigateUrl = "RepeatCards.aspx?Where=" + Repeatwhere;
            }
            else
            {
                Establishment.Text = "";
                //已使用
                //string UseWhere = Where + " and  len(CardID)=8 and left(CardID,4)='" + card.EmFrontCode + "' and convert(int,right(CardID,4),4)>= " + Convert.ToInt32(card.EmStartNum) + " and convert(int,right(CardID,4),4)<=" + Convert.ToInt32(card.EmEndNum);
                //int userCount = soldier.GetSoldiersCountByWhere(UseWhere);

                UseCount.Text = "";
                UseCount.NavigateUrl = "";
                //未使用
                NoUserCount.Text = "";
                NoUserCount.NavigateUrl = "";
                //不正确
                //string ErrorWhere = " isDelete=0 and iscontract=1 and left(departmentid,4)='" + departmentid.Substring(0, 4) + "' and (len(CardID)<>8  or convert(int,right(CardID,4),4)< " + card.EmStartNum + " or convert(int,right(CardID,4),4)>" + card.EmEndNum + " or left(CardID,4)<>'" + card.EmFrontCode + "' )";

                //int errorCount = soldier.GetSoldiersCountByWhere(ErrorWhere);
                ErrorCount.Text = "";
                ErrorCount.NavigateUrl = "";
                gvSoldiers.DataSource = null;
                gvSoldiers.DataBind();
            }

            //编码规则
            CardCodeInfo cardcode = CardCode.GetCardCodeInfoByDepartmentID(ddlDepartment.SelectedValue);
            lblEstablishment.Text = cardcode.ContractEstablishment.ToString();
            lblFrontCode.Text = cardcode.EmFrontCode;
            StartNum.Text = cardcode.EmStartNum;
            EndNum.Text = cardcode.EmEndNum;

        }
        else
        {
            gvSoldiers.DataSource = null;
            gvSoldiers.DataBind();
            Establishment.Text = "";
            UseCount.Text = "";
            NoUserCount.Text = "";
            ErrorCount.Text = "";

            //编码规则
            lblEstablishment.Text = "";
            lblFrontCode.Text = "";
            StartNum.Text = "";
            EndNum.Text = "";
        }
    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        BindGV(PageIndex);
    }
    protected void gvSoldiers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = Convert.ToString(gvSoldiers.PageSize * ListPageContral1.CurrentPageIndex + (e.Row.RowIndex + 1));
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            SoldierArchiveInfo info = e.Row.DataItem as SoldierArchiveInfo;
            //入伍日期
            Label lblJoinTime = e.Row.FindControl("lblJoinTime") as Label;
            lblJoinTime.Text = info.JoinTime.Year == 9999 ? "" : info.JoinTime.ToString("yyyy.MM");
            Label lblBirthday = e.Row.FindControl("lblBirthday") as Label;
            lblBirthday.Text = info.Birthday.Year == 9999 ? "" : info.Birthday.ToString("yyyy.MM");
        }
    }
    protected void gvSoldiers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListPageContral1.SetFirstPage();
        BindGV(0);
    }
}
